<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>js的原型链和继承 | 地球反三体组织</title>
    <meta name="generator" content="VuePress 1.7.1">
    <link rel="icon" href="/icons/favicon.ico">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.slim.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.2/jquery.fancybox.min.js"></script>
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.2/jquery.fancybox.min.css">
    <meta name="description" content="">
    
    <link rel="preload" href="/assets/css/0.styles.95f09eca.css" as="style"><link rel="preload" href="/assets/js/app.892efb48.js" as="script"><link rel="preload" href="/assets/js/3.3689c535.js" as="script"><link rel="preload" href="/assets/js/1.76946483.js" as="script"><link rel="preload" href="/assets/js/33.e9f606e7.js" as="script"><link rel="prefetch" href="/assets/js/10.994dfc46.js"><link rel="prefetch" href="/assets/js/11.5243d02b.js"><link rel="prefetch" href="/assets/js/12.bbc947db.js"><link rel="prefetch" href="/assets/js/13.3c555110.js"><link rel="prefetch" href="/assets/js/14.791ae1f2.js"><link rel="prefetch" href="/assets/js/15.41a71b1f.js"><link rel="prefetch" href="/assets/js/16.ea9f6e56.js"><link rel="prefetch" href="/assets/js/17.8d9b3f45.js"><link rel="prefetch" href="/assets/js/18.990bf501.js"><link rel="prefetch" href="/assets/js/19.37c1c21a.js"><link rel="prefetch" href="/assets/js/20.5aae01e6.js"><link rel="prefetch" href="/assets/js/21.41e758f1.js"><link rel="prefetch" href="/assets/js/22.afc2de9d.js"><link rel="prefetch" href="/assets/js/23.7a76c57a.js"><link rel="prefetch" href="/assets/js/24.ee89a782.js"><link rel="prefetch" href="/assets/js/25.aae5f43e.js"><link rel="prefetch" href="/assets/js/26.eec74c57.js"><link rel="prefetch" href="/assets/js/27.cdae5890.js"><link rel="prefetch" href="/assets/js/28.85c693b7.js"><link rel="prefetch" href="/assets/js/29.d40f3724.js"><link rel="prefetch" href="/assets/js/30.cdb0bfe0.js"><link rel="prefetch" href="/assets/js/31.f83a512c.js"><link rel="prefetch" href="/assets/js/32.87779683.js"><link rel="prefetch" href="/assets/js/34.6b5688ff.js"><link rel="prefetch" href="/assets/js/35.a0ba89c1.js"><link rel="prefetch" href="/assets/js/36.18a00568.js"><link rel="prefetch" href="/assets/js/37.fd83be59.js"><link rel="prefetch" href="/assets/js/38.cf0ec149.js"><link rel="prefetch" href="/assets/js/39.3a424296.js"><link rel="prefetch" href="/assets/js/4.4ef4e9bb.js"><link rel="prefetch" href="/assets/js/40.3ac4f22a.js"><link rel="prefetch" href="/assets/js/41.f4119124.js"><link rel="prefetch" href="/assets/js/42.926201e5.js"><link rel="prefetch" href="/assets/js/43.b8af425d.js"><link rel="prefetch" href="/assets/js/44.b5af9721.js"><link rel="prefetch" href="/assets/js/45.2cf05349.js"><link rel="prefetch" href="/assets/js/46.6ef53d5a.js"><link rel="prefetch" href="/assets/js/47.0c0d1d6f.js"><link rel="prefetch" href="/assets/js/48.afcf25ca.js"><link rel="prefetch" href="/assets/js/49.56f7da91.js"><link rel="prefetch" href="/assets/js/5.152764ca.js"><link rel="prefetch" href="/assets/js/50.6dbb0658.js"><link rel="prefetch" href="/assets/js/6.8ab23831.js"><link rel="prefetch" href="/assets/js/7.b1119581.js"><link rel="prefetch" href="/assets/js/8.ebb978a3.js"><link rel="prefetch" href="/assets/js/9.a72af8f5.js">
    <link rel="stylesheet" href="/assets/css/0.styles.95f09eca.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container no-sidebar" data-v-73b6f1b4><div data-v-73b6f1b4><div id="loader-wrapper" class="loading-wrapper" data-v-d48f4d20 data-v-73b6f1b4 data-v-73b6f1b4><div class="loader-main" data-v-d48f4d20><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-58d5ab76 data-v-73b6f1b4 data-v-73b6f1b4><h3 class="title" style="display:none;" data-v-58d5ab76 data-v-58d5ab76>地球反三体组织</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-58d5ab76 data-v-58d5ab76><input type="password" value="" data-v-58d5ab76> <span data-v-58d5ab76>Konck! Knock!</span> <button data-v-58d5ab76>OK</button></label> <div class="footer" style="display:none;" data-v-58d5ab76 data-v-58d5ab76><span data-v-58d5ab76><i class="iconfont reco-theme" data-v-58d5ab76></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-58d5ab76>vuePress-theme-reco</a></span> <span data-v-58d5ab76><i class="iconfont reco-copyright" data-v-58d5ab76></i> <a data-v-58d5ab76><span data-v-58d5ab76>kuangw</span>
            
          <!---->
          2020
        </a></span></div></div> <div class="hide" data-v-73b6f1b4><header class="navbar" data-v-73b6f1b4><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <div class="pt-bgBar"></div> <a href="/" class="home-link router-link-active"><img src="/images/logo.png" alt="地球反三体组织" class="logo"> <span class="site-name">地球反三体组织</span></a> <div class="links"><div><div class="my-color isDrak"><div class="div1"><img src="http://photo.tuituisoft.com/picgo/20201202095920.png" alt></div> <div class="div2"><img src="http://photo.tuituisoft.com/picgo/20201202095945.png" alt></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont iconfont iconhome"></i>
  Home
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      Category
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/book/" class="nav-link"><i class="iconfont undefined"></i>
  book
</a></li><li class="dropdown-item"><!----> <a href="/categories/code/" class="nav-link"><i class="iconfont undefined"></i>
  code
</a></li><li class="dropdown-item"><!----> <a href="/categories/tool/" class="nav-link"><i class="iconfont undefined"></i>
  tool
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  Tag
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont undefined"></i>
      工具
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/tool/vuepress/" class="nav-link"><i class="iconfont undefined"></i>
  vuepress(博客框架)
</a></li><li class="dropdown-item"><!----> <a href="/tool/picgo.html" class="nav-link"><i class="iconfont undefined"></i>
  PicGo(图床工具)
</a></li><li class="dropdown-item"><h4>vscode</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/tool/vscode/plug-recommend.html" class="nav-link"><i class="iconfont undefined"></i>
  vscode插件推荐
</a></li><li class="dropdown-subitem"><a href="/tool/vscode/diy-code.html" class="nav-link"><i class="iconfont undefined"></i>
  如何自定义vscode的代码块
</a></li><li class="dropdown-subitem"><a href="/tool/vscode/code-block.html" class="nav-link"><i class="iconfont undefined"></i>
  常用代码块
</a></li></ul></li></ul></div></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div> <a href="https://gitee.com/kuangweiwudi/my-node" target="_blank" rel="noopener noreferrer" class="repo-link"><i class="iconfont reco-github"></i>
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask" data-v-73b6f1b4></div> <aside class="sidebar" data-v-73b6f1b4><div class="personal-info-wrapper" data-v-3eba7259 data-v-73b6f1b4><img src="http://image.woai996.com/picgo/20201127101131.png" alt="author-avatar" class="personal-img" data-v-3eba7259> <h3 class="name" data-v-3eba7259>
    kuangw
  </h3> <div class="num" data-v-3eba7259><div data-v-3eba7259><h3 data-v-3eba7259>29</h3> <h6 data-v-3eba7259>Article</h6></div> <div data-v-3eba7259><h3 data-v-3eba7259>22</h3> <h6 data-v-3eba7259>Tag</h6></div></div> <hr data-v-3eba7259></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont iconfont iconhome"></i>
  Home
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      Category
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/book/" class="nav-link"><i class="iconfont undefined"></i>
  book
</a></li><li class="dropdown-item"><!----> <a href="/categories/code/" class="nav-link"><i class="iconfont undefined"></i>
  code
</a></li><li class="dropdown-item"><!----> <a href="/categories/tool/" class="nav-link"><i class="iconfont undefined"></i>
  tool
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  Tag
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont undefined"></i>
      工具
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/tool/vuepress/" class="nav-link"><i class="iconfont undefined"></i>
  vuepress(博客框架)
</a></li><li class="dropdown-item"><!----> <a href="/tool/picgo.html" class="nav-link"><i class="iconfont undefined"></i>
  PicGo(图床工具)
</a></li><li class="dropdown-item"><h4>vscode</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/tool/vscode/plug-recommend.html" class="nav-link"><i class="iconfont undefined"></i>
  vscode插件推荐
</a></li><li class="dropdown-subitem"><a href="/tool/vscode/diy-code.html" class="nav-link"><i class="iconfont undefined"></i>
  如何自定义vscode的代码块
</a></li><li class="dropdown-subitem"><a href="/tool/vscode/code-block.html" class="nav-link"><i class="iconfont undefined"></i>
  常用代码块
</a></li></ul></li></ul></div></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div> <a href="https://gitee.com/kuangweiwudi/my-node" target="_blank" rel="noopener noreferrer" class="repo-link"><i class="iconfont reco-github"></i>
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav> <!----> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-58d5ab76 data-v-73b6f1b4><h3 class="title" style="display:none;" data-v-58d5ab76 data-v-58d5ab76>js的原型链和继承</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-58d5ab76 data-v-58d5ab76><input type="password" value="" data-v-58d5ab76> <span data-v-58d5ab76>Konck! Knock!</span> <button data-v-58d5ab76>OK</button></label> <div class="footer" style="display:none;" data-v-58d5ab76 data-v-58d5ab76><span data-v-58d5ab76><i class="iconfont reco-theme" data-v-58d5ab76></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-58d5ab76>vuePress-theme-reco</a></span> <span data-v-58d5ab76><i class="iconfont reco-copyright" data-v-58d5ab76></i> <a data-v-58d5ab76><span data-v-58d5ab76>kuangw</span>
            
          <!---->
          2020
        </a></span></div></div> <div data-v-73b6f1b4><main class="page"><div class="page-title" style="display:none;position:relative;"><div class="page-bg-img"><img src="https://w.wallhaven.cc/full/wq/wallhaven-wqo5er.png"></div> <h1>js的原型链和继承</h1> <hr> <div data-v-523cfaee><i class="iconfont reco-account" data-v-523cfaee><span data-v-523cfaee>kuangw</span></i> <i class="iconfont reco-date" data-v-523cfaee><span data-v-523cfaee>2020-11-03</span></i> <i class="iconfont reco-eye" data-v-523cfaee><span id="/js/prototype.html" data-flag-title="Your Article Title" class="leancloud-visitors" data-v-523cfaee><a class="leancloud-visitors-count" style="font-size:.9rem;font-weight:normal;color:#d6dce5;"></a></span></i> <i class="iconfont reco-tag tags" data-v-523cfaee><span class="tag-item" data-v-523cfaee>js</span><span class="tag-item" data-v-523cfaee>前端</span></i></div></div> <div class="theme-reco-content content__default" style="display:none;"><h1 id="学习js的原型链和继承"><a href="#学习js的原型链和继承" class="header-anchor">#</a> 学习js的原型链和继承</h1> <h2 id="_1-构造函数中的原型链"><a href="#_1-构造函数中的原型链" class="header-anchor">#</a> 1 构造函数中的原型链</h2> <h3 id="_1-1-函数"><a href="#_1-1-函数" class="header-anchor">#</a> 1.1 函数</h3> <ul><li>函数也是一个对象</li></ul> <div class="language-javascript extra-class"><pre class="language-javascript"><code>
<span class="token keyword">function</span> <span class="token constant">F</span><span class="token punctuation">(</span><span class="token parameter">a<span class="token punctuation">,</span> b</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
   	<span class="token keyword">return</span> a <span class="token operator">*</span> b<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token constant">F</span><span class="token punctuation">.</span>length<span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">// 2  指函数参数的个数</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token class-name">F</span><span class="token punctuation">.</span>constructor<span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">// function Function() { [native code] }</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">typeof</span> <span class="token class-name">F</span><span class="token punctuation">.</span>prototype<span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">// &quot;object&quot;</span>
</code></pre></div><ul><li>函数拥有属性，prototype也是一个对象，函数中的一个对象</li></ul> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token comment">// 这里我们把F这个函数当成一个对象，而这个对象中有个prototype 的对象  当然这是一个抽象的概念 下面的代码也只是一个比喻</span>
<span class="token constant">F</span> <span class="token operator">=</span> <span class="token punctuation">{</span>
    prototype<span class="token operator">:</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><h3 id="_1-2-prototype对象的属性"><a href="#_1-2-prototype对象的属性" class="header-anchor">#</a> 1.2 prototype对象的属性</h3> <ul><li>在prototype这个对象中，有个属性是constructo（构造函数），默认指向当前函数</li></ul> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token constant">F</span> <span class="token operator">=</span> <span class="token punctuation">{</span>
    prototype<span class="token operator">:</span> <span class="token punctuation">{</span>
        constructor<span class="token operator">:</span> <span class="token constant">F</span>    <span class="token comment">// 指向当前函数</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><ul><li>既然prototype是个对象，那我们也同样可以给它添加属性</li></ul> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token class-name">F</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>name <span class="token operator">=</span> <span class="token string">'李四'</span><span class="token punctuation">;</span>

<span class="token comment">// 那F就变成如下：</span>
<span class="token constant">F</span> <span class="token operator">=</span> <span class="token punctuation">{</span>
    prototype<span class="token operator">:</span> <span class="token punctuation">{</span>
        constructor<span class="token operator">:</span> <span class="token constant">F</span><span class="token punctuation">,</span>
        name<span class="token operator">:</span> <span class="token string">'李四'</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

</code></pre></div><h3 id="_1-3-创造对象"><a href="#_1-3-创造对象" class="header-anchor">#</a> 1.3 创造对象</h3> <ul><li>构造函数 我们通常把构造函数的第一个字母大写 而且构造函数中，会默认返回this</li></ul> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">let</span> f <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">F</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>f<span class="token punctuation">)</span>  <span class="token comment">// 这里我们得到一个空对象 {}？</span>
</code></pre></div><ul><li>其实不是 我们打开这个空对象一看，里面有个__proto__的属性  ，这个新对象的__proto__指向的就是构造函数中的prototype属性</li></ul> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token comment">//在这里 我们试试__proto__的作用</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>f<span class="token punctuation">.</span>__proto__<span class="token punctuation">)</span>
<span class="token comment">/*
输出
{
	name: &quot;李四&quot;
	constructor: ƒ F()
}
*/</span>
<span class="token comment">//既然 __proto__ = prototype ，那么也是有constructor这个函数 </span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>f<span class="token punctuation">.</span><span class="token class-name">__proto__</span><span class="token punctuation">.</span>constructor<span class="token punctuation">)</span>

<span class="token comment">//输出这个函数时，指向的是构造函数本身</span>


console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>f<span class="token punctuation">.</span>constructor<span class="token punctuation">)</span>

<span class="token comment">// 这里有点需要注意 输出f.constructor时，得到的结果和f.__proto__.constructor是一样的</span>

</code></pre></div><ul><li><code>f.__proto__.constructor</code> 和 <code>f.constructor</code> 的区别</li></ul> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token comment">//实际上他们两就是一个东西  __proto__这个属性。将constructor共享到子对象了。当构造函数的属性发生变化是，子对象也会跟着动态变化</span>
<span class="token constant">F</span> <span class="token operator">=</span> <span class="token punctuation">{</span>
    prototype<span class="token operator">:</span> <span class="token punctuation">{</span>
        constructor<span class="token operator">:</span> <span class="token constant">F</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

f1 <span class="token operator">=</span> <span class="token punctuation">{</span>
    age<span class="token operator">:</span> <span class="token number">18</span><span class="token punctuation">,</span>
    __proto__<span class="token operator">:</span> <span class="token punctuation">{</span>    <span class="token comment">// 既然我们已经把这个形象化为&quot;共享&quot;属性了，那就再形象一点</span>
        constructor<span class="token operator">:</span> <span class="token constant">F</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment">// 更形象化：</span>
f1 <span class="token operator">=</span> <span class="token punctuation">{</span>
    age<span class="token operator">:</span> <span class="token number">18</span><span class="token punctuation">,</span>  <span class="token comment">// 这个是f1对象自身属性</span>
    constructor<span class="token operator">:</span> <span class="token constant">F</span>  <span class="token comment">// 这个是从原型上&quot;共享&quot;的属性</span>
<span class="token punctuation">}</span>

</code></pre></div><ul><li>尝试去更改原型链属性</li></ul> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token class-name">F</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>name <span class="token operator">=</span> <span class="token string">'张三'</span>

<span class="token keyword">let</span> f  <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">F</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>f<span class="token punctuation">.</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//输出张三</span>

<span class="token class-name">F</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>name <span class="token operator">=</span> <span class="token string">'李四'</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>f<span class="token punctuation">.</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//输出李四</span>

<span class="token comment">//注意 当我们仅仅只是打印f这个对象时，会发现依然是个空对象 ，这个name的属性，是从原型链上拿过来的</span>
</code></pre></div><h3 id="_1-4-对象自身的属性和原型链的属性"><a href="#_1-4-对象自身的属性和原型链的属性" class="header-anchor">#</a> 1.4 对象自身的属性和原型链的属性</h3> <ul><li>当使用<code>hasOwnProperty</code>去监测name这个属性时，发现会得到false，说明name这个属性不是自身的属性</li></ul> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">function</span> <span class="token constant">F</span><span class="token punctuation">(</span><span class="token parameter">a</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token keyword">this</span><span class="token punctuation">.</span>age <span class="token operator">=</span> a
<span class="token punctuation">}</span>
<span class="token class-name">F</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>name <span class="token operator">=</span> <span class="token string">'张三'</span>
<span class="token keyword">let</span> f  <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">F</span><span class="token punctuation">(</span><span class="token number">18</span><span class="token punctuation">)</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>f<span class="token punctuation">.</span><span class="token function">hasOwnProperty</span><span class="token punctuation">(</span><span class="token string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//输出false 不是自身的属性</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>f<span class="token punctuation">.</span><span class="token function">hasOwnProperty</span><span class="token punctuation">(</span><span class="token string">'age'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//输出true  是自身的属性</span>
    
</code></pre></div><ul><li>尝试删除原型链属性</li></ul> <div class="language- extra-class"><pre class="language-text"><code>delete f.age;
console.log(f.age); // undefined

delete f.name;
console.log(f.name); // '张三'

</code></pre></div><p>原型链上的属性，无法在子对象上删除</p> <ul><li>js属性的查找顺序,当我们使用一个属性时，js会优先的去自己本身找这个属性，如果找不到，就会去原型链上找</li></ul> <h2 id="_2-继承"><a href="#_2-继承" class="header-anchor">#</a> 2 继承</h2> <h2 id="_2-1-原型链"><a href="#_2-1-原型链" class="header-anchor">#</a> 2.1 原型链</h2> <p>当一个原型链指向另一个原型链的时候，这时候他们就</p> <h2 id="_2-2继承"><a href="#_2-2继承" class="header-anchor">#</a> 2.2继承</h2> <div class="language- extra-class"><pre class="language-text"><code> //先定义三个构造函数

// 构造函数A
function A() {
	this.name = 'A';
};
A.prototype.say = function () {
	return this.name;
};
// 构造函数B
function B() {
	this.name = 'B';
};
// 构造函数C
function C(width, height) {
	this.name = 'C';
	this.width = width;
	this.height = height;
	this.getArea = function () {
		return this.width * this.height;
	};
};

// 我们让B的原型链指向A 
B.prototype = new A();

// 再让C的原型链指向B 
C.prototype = new B();
let c1 = new C(2, 6);
console.log(c1);   // {name: &quot;C&quot;, width: 2, height: 6, getArea: ƒ}
console.log(c1.name);  // 'C'
console.log(c1.getArea()); // 12
console.log(c1.say());  // 'C'

//这时候我们会发现  C居然也有了say这个函数了  这是因为b继承了 a  b有了say这个方法 而c继承了b   所有c也有了
</code></pre></div><div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token comment">// 这里我们会发现一个问题  当我们打印b的指向时， 会发现输出的都是A的构造函数</span>
   <span class="token comment">// 这里我们会发现一个问题  当我们打印b和c的指向时， 会发现输出的都是A的构造函数</span>
   console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token class-name">B</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>constructor<span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">// ƒ A() {}</span>
   <span class="token keyword">let</span> b1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">B</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
   console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>b1<span class="token punctuation">.</span>constructor<span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">// ƒ A() {}</span>
   console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token class-name">C</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>constructor<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// ƒ A() {}</span>
</code></pre></div><ul><li>为什么会出现这种情况</li></ul> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token comment">//我们用抽象化来解释一下</span>
<span class="token constant">A</span> <span class="token operator">=</span> <span class="token punctuation">{</span>
	name<span class="token operator">:</span> <span class="token string">'A'</span><span class="token punctuation">,</span>
	<span class="token function-variable function">say</span><span class="token operator">:</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token keyword">this</span><span class="token punctuation">.</span>name<span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    prototype：<span class="token punctuation">{</span>
        constructor<span class="token operator">:</span> <span class="token constant">A</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token constant">B</span> <span class="token operator">=</span> <span class="token punctuation">{</span>
	name<span class="token operator">:</span><span class="token string">'B'</span>
    prototype：<span class="token punctuation">{</span>
        constructor<span class="token operator">:</span> <span class="token constant">B</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token class-name">B</span><span class="token punctuation">.</span>prototype <span class="token operator">=</span> <span class="token constant">A</span>
<span class="token constant">B</span> <span class="token operator">=</span> <span class="token punctuation">{</span>
	name<span class="token operator">:</span><span class="token string">'B'</span>
    prototype：<span class="token punctuation">{</span>
        name<span class="token operator">:</span> <span class="token string">'A'</span><span class="token punctuation">,</span>
		<span class="token function-variable function">say</span><span class="token operator">:</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
			<span class="token keyword">return</span> <span class="token keyword">this</span><span class="token punctuation">.</span>name<span class="token punctuation">;</span>
		<span class="token punctuation">}</span><span class="token punctuation">,</span>
		prototype：<span class="token punctuation">{</span>
			constructor<span class="token operator">:</span> <span class="token constant">A</span>
		<span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token class-name">B</span><span class="token punctuation">.</span>constructor <span class="token operator">=</span> <span class="token constant">A</span>
</code></pre></div><ul><li>如果要解决这个问题，我需要手动调整<code>B.prototype.constructor</code>的指向 ，c也是一样的</li></ul> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token class-name">B</span><span class="token punctuation">.</span>prototype <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">A</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">B</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>constructor <span class="token operator">=</span> <span class="token constant">B</span><span class="token punctuation">;</span>
<span class="token class-name">C</span><span class="token punctuation">.</span>prototype <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">B</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">C</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>constructor <span class="token operator">=</span> <span class="token constant">C</span><span class="token punctuation">;</span>
</code></pre></div><ul><li>效率的问题</li></ul> <p>当我们用某一个构造函数创建对象时，其属性就会被添加到this中去。并且当别添加的属性实际上是不会随着实例改变时，这种做法会显得没有效率。例如在上面的实例中，A构造函数是这样定义的：</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">function</span> <span class="token constant">A</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> <span class="token string">'A'</span><span class="token punctuation">;</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function-variable function">say</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token keyword">this</span><span class="token punctuation">.</span>name<span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">//这种方式 每new一个A 都会创造出一个name的属性 和say的方法，独占了一份内存</span>


<span class="token keyword">function</span> <span class="token constant">A</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
	<span class="token class-name">A</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>name <span class="token operator">=</span> <span class="token string">'A'</span><span class="token punctuation">;</span>
	<span class="token class-name">A</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function-variable function">say</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">return</span> <span class="token keyword">this</span><span class="token punctuation">.</span>name<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">// 如果我们使用原型链的方式  所有实例出来的A 都是指向同一个name属性和say方法 达到优化性能的目的</span>
<span class="token comment">// 但如果如果是一些经常可变的属性，则不适用于放在原型链上，我们要考虑应该将哪些属性放于原型链上更好</span>

</code></pre></div><ul><li>基于原型的方式继承</li></ul> <p>处于效率考虑，我们应当尽可能的将一些可重用的属性和方法添加到原型中去，这样的话我们仅仅依靠原型就可以完成继承关系的构建了，由于原型上的属性都是可重用的，这也意味着从原型上继承比在实例上继承要好得多，而且既然需要继承的属性都放在了原型上，又何必生成实例降低效率，然后又从所生成的实例中继承不需要的私有属性呢？所以我们直接抛弃实例，从原型上继承：</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">function</span> <span class="token constant">A</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token class-name">A</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>name <span class="token operator">=</span> <span class="token string">'A'</span><span class="token punctuation">;</span>
<span class="token class-name">A</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function-variable function">say</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token keyword">this</span><span class="token punctuation">.</span>name<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">// 构造函数B</span>
<span class="token keyword">function</span> <span class="token constant">B</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token class-name">B</span><span class="token punctuation">.</span>prototype <span class="token operator">=</span> <span class="token class-name">A</span><span class="token punctuation">.</span>prototype<span class="token punctuation">;</span>  <span class="token comment">//  先继承，再进行constructor矫正和B.prototype的扩展</span>
<span class="token class-name">B</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>constructor <span class="token operator">=</span> <span class="token constant">B</span><span class="token punctuation">;</span>
<span class="token class-name">B</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>name <span class="token operator">=</span> <span class="token string">'B'</span><span class="token punctuation">;</span>

<span class="token comment">// 构造函数C</span>
<span class="token keyword">function</span> <span class="token constant">C</span><span class="token punctuation">(</span><span class="token parameter">width<span class="token punctuation">,</span> height</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>  <span class="token comment">// 此处的width和height属性是随参数变化的，所以就不需要改为共享属性</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>width <span class="token operator">=</span> width<span class="token punctuation">;</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>height <span class="token operator">=</span> height<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token class-name">C</span><span class="token punctuation">.</span>prototype <span class="token operator">=</span> <span class="token class-name">B</span><span class="token punctuation">.</span>prototype<span class="token punctuation">;</span>
<span class="token class-name">C</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>constructor <span class="token operator">=</span> <span class="token constant">C</span><span class="token punctuation">;</span> <span class="token comment">//  先继承，再进行constructor矫正和C.prototype的扩展</span>
<span class="token class-name">C</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>name <span class="token operator">=</span> <span class="token string">'C'</span><span class="token punctuation">;</span>
<span class="token class-name">C</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function-variable function">getArea</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token keyword">this</span><span class="token punctuation">.</span>width <span class="token operator">*</span> <span class="token keyword">this</span><span class="token punctuation">.</span>height<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><ul><li>这样的代码似乎更加优雅了,试试效果</li></ul> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">let</span> b2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">B</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>b2<span class="token punctuation">.</span><span class="token function">say</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">// 'C'</span>

<span class="token comment">// 这里会发现输出的是C不是我们 想要的效果</span>
</code></pre></div></div> <footer class="page-edit" style="display:none;"><div class="edit-link"><a href="https://gitee.com/kuangweiwudi/my-node/edit/master/js/prototype.md" target="_blank" rel="noopener noreferrer">提出错误！</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <!----></footer> <!----> <!----> <ul class="side-bar sub-sidebar-wrapper" style="width:12rem;" data-v-3fb51744><li class="level-2" data-v-3fb51744><a href="/js/prototype.html#_1-构造函数中的原型链" class="sidebar-link" data-v-3fb51744>1 构造函数中的原型链</a></li><li class="level-3" data-v-3fb51744><a href="/js/prototype.html#_1-1-函数" class="sidebar-link" data-v-3fb51744>1.1 函数</a></li><li class="level-3" data-v-3fb51744><a href="/js/prototype.html#_1-2-prototype对象的属性" class="sidebar-link" data-v-3fb51744>1.2 prototype对象的属性</a></li><li class="level-3" data-v-3fb51744><a href="/js/prototype.html#_1-3-创造对象" class="sidebar-link" data-v-3fb51744>1.3 创造对象</a></li><li class="level-3" data-v-3fb51744><a href="/js/prototype.html#_1-4-对象自身的属性和原型链的属性" class="sidebar-link" data-v-3fb51744>1.4 对象自身的属性和原型链的属性</a></li><li class="level-2" data-v-3fb51744><a href="/js/prototype.html#_2-继承" class="sidebar-link" data-v-3fb51744>2 继承</a></li><li class="level-2" data-v-3fb51744><a href="/js/prototype.html#_2-1-原型链" class="sidebar-link" data-v-3fb51744>2.1 原型链</a></li><li class="level-2" data-v-3fb51744><a href="/js/prototype.html#_2-2继承" class="sidebar-link" data-v-3fb51744>2.2继承</a></li></ul></main> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div></div></div>
    <script src="/assets/js/app.892efb48.js" defer></script><script src="/assets/js/3.3689c535.js" defer></script><script src="/assets/js/1.76946483.js" defer></script><script src="/assets/js/33.e9f606e7.js" defer></script>
  </body>
</html>
